#include "home_view.h"
#include "ui_home_view.h"

#include "activity_view.h"
#include <QListWidgetItem>
#include <QMessageBox>

HomeView::HomeView(QWidget *parent) :
    ViewBase(parent),
    ui(new Ui::HomeView),
    activityView_(0)
{
    ui->setupUi(this);


    connect(ui->lstNews, SIGNAL(itemActivated(QListWidgetItem*)),
            this, SLOT(showActivity(QListWidgetItem*)));

    // Create push buttons for place and activity tags;
    for (int i = 0; i < 8; ++i) {
      btnPlaceTags[i] = new QPushButton(ui->grpLocationTags);
      btnActivityTags[i] = new QPushButton(ui->grpActivityTags);

      btnPlaceTags[i]->move((i % 4) * 60 + 5, i / 4 * 30 + 10);
      btnActivityTags[i]->move((i % 4) * 60 + 5, i / 4 * 30 + 10);
      btnPlaceTags[i]->setFixedSize(55, 25);
      btnActivityTags[i]->setFixedSize(55, 25);
    }

    QAction *backSoftkeyAction = new QAction(QString("Back"), this);
    backSoftkeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
    QObject::connect(backSoftkeyAction, SIGNAL(triggered()),
                     this, SLOT(close()));
    addAction(backSoftkeyAction);
}

HomeView::~HomeView()
{
    delete ui;
    if (activityView_) {
        delete activityView_;
    }
}

void HomeView::IssueRequest() {
  RequestMethod request(this, "xml", "App.getHomeRecordsAndTags");
  request.AddParam(QString("city"), QString("北京"));
  request.AddParam(QString("lang"), kLanguage);

  homeActivities.clear();
  placeTags.clear();
  activityTags.clear();

  ui->lstNews->clear();

  if (handler_) {
    handler_->IssueRequest(request);
  }
}


int HomeView::ProcessResult(RequestMethod &r, QDomElement &e) {
  int re = parse_result(e);

  if (re == 0) {
    // Add home news
    for (QVector<activity>::iterator it = homeActivities.begin();
        it != homeActivities.end(); ++it) {
          activity &a = *it;
      ui->lstNews->addItem(a.name);
    }
    // Add buttons for place tags.
    for (int i = 0; i < placeTags.count(); ++i) {
      tag &t = placeTags[i];
      btnPlaceTags[i]->setText(t.tag_name);
    }
    // Add buttons for activity tags
    for (int i = 0; i < activityTags.count(); ++i) {
      tag &t = activityTags[i];
      btnActivityTags[i]->setText(t.tag_name);
    }
  }

  return re;
}

int HomeView::parse_result(QDomElement &e) {
  QDomNodeList l = e.childNodes();
  for (int i = 0; i < l.count(); ++i) {
    QDomElement el = l.at(i).toElement();
    if (el.nodeName() == QString("activities")) {
      parse_activities(el);
    }
    if (el.nodeName() == QString("place_tags")) {
      parse_place_tags(el);
    }
    if (el.nodeName() == QString("activity_tags")) {
      parse_activity_tags(el);
    }
  }

  return 0;
}

int HomeView::parse_activities(QDomElement &e) {
  QDomNodeList l = e.childNodes();
  for (int i = 0; i < l.count(); ++i) {
    QDomElement el = l.at(i).toElement();
    if (el.nodeName() == QString("activity")) {
      activity a;
      parse_activity(el, a);
      homeActivities.push_back(a);
    }
  }

  return 0;
}

int HomeView::parse_place_tags(QDomElement &e) {
  QDomNodeList l = e.childNodes();
  for (int i = 0; i < l.count(); ++i) {
    QDomElement el = l.at(i).toElement();
    if (el.nodeName() == QString("tag")) {
      tag t;
      t.tag_name = el.text();
      placeTags.push_back(t);
    }
  }

  return 0;
}

int HomeView::parse_activity_tags(QDomElement &e) {
  QDomNodeList l = e.childNodes();
  for (int i = 0; i < l.count(); ++i) {
    QDomElement el = l.at(i).toElement();
    if (el.nodeName() == QString("tag")) {
      tag t;
      t.tag_name = el.text();
      activityTags.push_back(t);
    }
  }

  return 0;
}

void HomeView::showActivity(QListWidgetItem* item) {
    for (int i = 0; i < homeActivities.count(); ++i) {
      activity &a = homeActivities[i];
      if (a.name == item->text()) {
        if (!activityView_) {
          activityView_ = new ActivityView(this);
          activityView_->SetRequestHandler(handler_);
        }
        activityView_->Show();
        activityView_->SetActivity(a);
        activityView_->IssueRequest();
      }
    }
}
